package com.ctg.payroll2.service.payment.impl;

import java.sql.SQLException;
import java.util.List;

import com.ctg.payroll2.bean.filter.payment.PayClazzItemFilter;
import com.ctg.payroll2.bean.po.payment.PayClassitem;
import com.ctg.payroll2.bean.po.payment.PayClassitemLimitDate;
import com.ctg.payroll2.dao.payment.PayClazzItemDao;
import com.ctg.payroll2.service.payment.PayClazzItemService;
import com.ctg.platform.util.Page;

public class PayClazzItemServiceImpl implements PayClazzItemService {
	private PayClazzItemDao dao;

	/**
	 * @param dao 设置dao
	 */
	public void setDao(PayClazzItemDao dao) {
		this.dao = dao;
	}
	/**
	 * 翻页查询薪资项目
	 * @param filter 过滤条件
	 * @return
	 */
	public Page getPayItemListByFilter(PayClazzItemFilter filter){
		return dao.getPayItemListByFilter(filter);
	}
	/**
	 * 翻页查询显示顺序薪资项目
	 * @param filter 过滤条件
	 * @return
	 */
	public List<PayClassitem> getDisplayPayItemListByFilter(PayClazzItemFilter filter){
		return dao.getDisplayPayItemListByFilter(filter);
	}
	public int deletePayItemByIds(String ids) throws SQLException{
		return dao.deletePayItemByIds(ids)+dao.fakeDeletePayItemByIds(ids);
	}
	public Long insertPayItem(PayClassitem payItem){
		Long id = dao.insertPayItem(payItem);//插入主表
		if (payItem.getDetailList()!=null&&payItem.getDetailList().size()>0) {
			for (PayClassitemLimitDate detail : payItem.getDetailList()) {
				detail.setPay_classitem_id(id);
				detail.setCompany_code(payItem.getCompany_code());
				detail.setCreator_code(payItem.getCreator_code());
				dao.insertTimeLimit(detail);//插入子表
			}
		}
		return id;
	}
	public List<PayClassitem> queryPayItemByFilter(PayClazzItemFilter filter){
		return dao.queryPayItemByFilter(filter);
	}
	public int modifyPayItem(PayClassitem payItem){//策略:先删除再插入
		//主表是更新操作
		int count = dao.modifyPayItemAll(payItem);
		dao.deleteTimeLimitByIds((payItem.getPay_classitem_id()).toString());//先删除所有的
		if (payItem.getDetailList()!=null&&payItem.getDetailList().size()>0) {
			for (PayClassitemLimitDate detail : payItem.getDetailList()) {
				detail.setPay_classitem_id(payItem.getPay_classitem_id());
				detail.setPay_class_id(Long.parseLong(payItem.getPay_class_id()));
				detail.setCompany_code(payItem.getCompany_code());
				detail.setCreator_code(payItem.getCreator_code());
				dao.insertTimeLimit(detail);//再插入子表
			}
		}
		return count;
	}
	
	public int updateValidAndDisplay(PayClassitem payItem){
		return dao.modifyPayItem(payItem);
	}
	public int validateValidAndDisplay(PayClassitem payItem){
		return dao.validateValidAndDisplay(payItem);
	}
	public int deleteTimeLimitByIds(String ids) {
		return dao.deleteTimeLimitByIds(ids);
	}
	public List<PayClassitemLimitDate> getTimeLimitByPayItemId(Long id) {
		return dao.getTimeLimitByPayItemId(id);
	}
	public Long insertTimeLimit(PayClassitemLimitDate limitTime) {
		return dao.insertTimeLimit(limitTime);
	}
	public int updateTimeLimit(PayClassitemLimitDate limitTime) {
		return dao.updateTimeLimit(limitTime);
	}
	
	@SuppressWarnings("unchecked")
	public List<PayClassitem> queryForFormula(PayClassitem payClassitem) {
		return dao.queryForFormula(payClassitem);
	}
	
	public String checkExistsInHis(String ids) throws SQLException{
		return dao.checkExistsInHis(ids);
	}
	public String checkExistsInFormula(String ids) throws SQLException{
		return dao.checkExistsInFormula(ids);
	}
	public int getNextSegmentOrder(PayClassitem payItem){
		List<Integer> list = dao.getNextSegmentOrder(payItem);
		
		int i0 = 51;
		if(list.size() != 0){
			int i1 = list.get(0);
			int i2 = list.get(list.size() - 1);
			if(i1 == i0 && i2 == i0 + list.size() - 1){
				i0 = i0 + list.size();
			}else{
				for(int i = 0; i < list.size(); i++){
					if(list.get(i) != i0 + i){
						i0 = i0 + i;
						break;
					}
				}
			}
		}
		return i0;
	}
	public String checkItemIsValid(PayClazzItemFilter filter) throws SQLException{
		return dao.checkItemIsValid(filter);
	}
}
